<?php

namespace App\Http\Controllers\Admin;

use App\Models\Type;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;

class TypeController extends Controller
{


    /**
     * 页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  index()
    {

        return view('/admin/type/index');
    }
    /**
     * 编辑
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  edit(Request $request)
    {
        $params = $request->only(['type_id']);
        $validator = \Validator::make($params, [
            'type_id' => 'required|exists:types,id',
        ], [
            'type_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $data = Type::find($request->type_id);

        return view('admin/type/edit',['data'=>$data]);
    }

    /**
     * 添加
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function  add()
    {


        return view('admin/type/add');
    }

    /**
     * 分类列表
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function data(Request $request)
    {
        $searchParams = json_decode($request->searchParams, true);
        $data = Type::orderBy('id','desc')->Title($searchParams['title']??'')->paginate($request->limit)->toArray();

        return $this->success(['data'=>$data['data'], 'count'=> $data['total'],'code'=>0]);
        return $this->success($data);
    }


    /**
     * 分类详情
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $params = $request->only(['type_id']);
        $validator = \Validator::make($params, [
            'type_id' => 'required|exists:types,id',
        ], [
            'type_id.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $type = Type::find($request->type_id);
        return $this->success($type);
    }

    /**
     * 修改
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function saveType(Request $request)
    {
        $params = $request->only(['type_id', 'title']);
        $validator = \Validator::make($params, [
            'type_id' => 'required|exists:types,id',
            'title' => ['required','string','max:20',Rule::unique('types')->ignore($request->type_id)],
        ], [
            'type_id.*' => '参数错误',
            'title.*' => '参数错误',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $re = Type::edit($request->type_id, $request->title);

        if ($re) {
            return $this->success(['msg' => '修改成功']);
        }
        return $this->error('修改失败');

    }

    /**
     * 新增
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addType(Request $request)
    {
        $params = $request->only(['title']);
        $validator = \Validator::make($params, [
            'title' => 'required|string|max:20|unique:types,title',
        ], [
            'title.required' => '名称参数必填',
            'title.unique' => '名称参数已存在',
            'title.max' => '名称参数长度已存在',
        ]);

        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }
        $data = [
            'title' => $request->title
        ];
        $re = Type::add($data);

        if ($re) {
            return $this->success(['msg' => '修改成功']);
        }
        return $this->error('修改失败');

    }

    /**
     * 删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function    del(Request $request)
    {
        $params = $request->only(['type_id']);
        $validator = \Validator::make($params, [
            'type_id' => 'required|exists:types,id',
        ], [
            'type_id.*' => '参数错误',
        ]);
        if ($validator->fails()) {
            return $this->error($validator->errors()->first());
        }

        $problems = Type::find($request->type_id);

        $problems->delete();
        if ($problems->trashed()) {
            return $this->success(['msg' => '保存成功']);
        }
        return $this->error('保存失败');


    }
}
